/*DIGITAL THERMOMETER by COLIN LOBO
  
  WHAT YOU NEED:
  1 - LITEON LTD-4608JR (Common Anode [+])
  10 - 330 OHM RESISTORS
  2 - 2N3906 TRANSISTORS (Type PNP)
  1 - 38GZ TEMPERATURE SENSOR
  
  REFERENCES:
  1. http://www.ladyada.net/learn/sensors/tmp36.html
  2. http://www.sparkfun.com/tutorial/AIK/ARDX-EG-SPAR-WEB.pdf (Page 26)
  3. http://www.tinkerhobby.com/arduino-2-digit-7-segment-display-counter/
  4. http://www.kpsec.freeuk.com/components/tran.htm (Transistors)
  
  Notes:
  If using a common cathode seven segment display use an NPN transistor!
*/

//PIN ASSIGNMENTS
#define digit1 10 //common anode for digit 1 connected to pin 8
#define digit2 11 //common anode for digit 2 connected to pin 9
#define tempPin 0 //Vout of TMP36 connected to pin A0
#define aref_voltage 3.3 //3.3V ref allows for better resolution and less noise

/*
This is just to subtract 2 degC degrees from reading. After testing it was found MY temp sensor was +4 degC above actual temperature.
The TMP36 has accuracy of +/-2 degC. If yours is greater than this than use this to adjust. Also, if your temperature sensor
varies quite a bit (jumps around in values too frequently) then you can take multiple readings, average them then display this new value.

#define offset 2

*/

byte led_display[7] = {2, 3, 4, 5, 6, 7, 8}; //A,B,C,D,E,F,G pins on LITEON assigned to corresponding 2,3,4,5,6,7,8 arduino pins

//Array for each number and corresponding state for each individual segment. Look at your datasheet for segment and pin arrangment.
byte seven_seg_digits[10][7] = {{0,0,0,0,0,0,1}, //0
                                {1,0,0,1,1,1,1}, //1
                                {0,0,1,0,0,1,0}, //2
                                {0,0,0,0,1,1,0}, //3
                                {1,0,0,1,1,0,0}, //4
                                {0,1,0,0,1,0,0}, //5
                                {0,1,0,0,0,0,0}, //6
                                {0,0,0,1,1,1,1}, //7
                                {0,0,0,0,0,0,0}, //8
                                {0,0,0,0,1,0,0}}; //9
                                
void setup() {
  //Set Pin Mode for each digital I/O pin for output and also give a default state so everything is off.
  byte i = 0;
  for (i; i < 7; i++)
  {
    pinMode(led_display[i], OUTPUT);
    digitalWrite(led_display[i], HIGH);
  }
  pinMode(digit1, OUTPUT);
  pinMode(digit2, OUTPUT);
  digitalWrite(digit1, LOW);
  digitalWrite(digit2, LOW);
  //Used only for changing the analog reference in this case to 3.3V instead of 5V!
  analogReference(EXTERNAL);
}

void digitWrite(byte digit) {
  byte segment;
  for(segment = 0; segment < 7; segment++)
  {
    //Given a digit between 0-9 set the state of the corresponding segments on the display using array.
    digitalWrite(led_display[segment], seven_seg_digits[digit][segment]);
  }
}

float getTemp() {
  int tempReading = analogRead(tempPin);
  float voltage = tempReading * aref_voltage;
  voltage /= 1024.0; //Convert reading (0-1024) to a voltage reading
  float temperature = (voltage - 0.5) * 100; //offset by 500mV to get negative temperatures. Multiply by 100 to get degC (each 10mV is 1 degC)
  return temperature;
}

void displayTemp(int temp_value) {
  if(temp_value < 10)
    {
      //We have single number so we don't need digit 1.
      digitalWrite(digit1, LOW);
      digitalWrite(digit2, HIGH);
      digitWrite(temp_value);
    }
  else if(9 < temp_value < 100)
    {
       //We have a two digit value so we need to break the digits up and write corresponding digits separatly.
       int first_digit = temp_value / 10;
       //Remainder is second digit
       int second_digit = temp_value % 10;
       //Write first digit
       digitalWrite(digit1, HIGH);
       digitalWrite(digit2, LOW);
       digitWrite(first_digit);
       //Delay is to prevent flickering
       delay(5);
       //Write second digit
       digitalWrite(digit1, LOW);
       digitalWrite(digit2, HIGH);
       digitWrite(second_digit);
       delay(5);
    }
  else
  {
       //Everything else. If here, something went wrong display 99.
       digitalWrite(digit1, HIGH);
       digitalWrite(digit2, LOW);
       digitWrite(9);
       digitalWrite(digit1, LOW);
       digitalWrite(digit2, HIGH);
       digitWrite(9);
  }
}

void loop() { 
  int temp_value = getTemp();
  displayTemp(temp_value);
}
